<?php
/**
 * +----------------------------------------------------------------------
 * | Copyright © Sogu Technologies Co. Ltd 2018-2050 All rights reserved.
 * +----------------------------------------------------------------------
 * | File name: OldRepaySummary.php
 * +----------------------------------------------------------------------
 * | Author: lijiafei
 * +----------------------------------------------------------------------
 * | Description:
 * +----------------------------------------------------------------------
 * | Others: 其他内容的说明
 * +----------------------------------------------------------------------
 * | History:
 * +----------------------------------------------------------------------
 * | 1.Date: 2019/2/27 Author: lijiafei Modification: 创建
 * +----------------------------------------------------------------------
 */

namespace App\Models\Traits;


use App\Models\RepayRecord;
use App\Models\RepayRecordCommon;
use App\Models\RepayRecordMoney;
use App\Models\RepayRecordMonth;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

trait OldRepaySummary
{

    /**
     * Description:计算农信的各个金额
     * Author:lijiafei
     * @param RepayRecord $recordInfo
     * @return int|string
     */
    public static function getOldMoney(RepayRecord $recordInfo){
        $money = 0;
        $money = bcadd($money,$recordInfo->amount,2);//record表的本金
        $money = bcadd($money,$recordInfo->money->lending_money,2);//资金的利息
        $money = bcadd($money,$recordInfo->month->amountt,2);//月保费
        $money = bcadd($money,$recordInfo->common->lending_money,2);//公共的利息
        $money = bcadd($money,$recordInfo->common->manage_money,2);//公共的管理费
        $money = bcadd($money,$recordInfo->common->fines_money ,2);//公共的罚息
        return $money;
    }

    /**
     * Description:计算农信的提前结清的金额
     * Author:lijiafei
     * @param $summary_id
     * @return mixed
     */
    public static function getOldRepayMoney($summary_id){
        $all_money = 0;
        $money = RepayRecord::select() -> where('summary_id',$summary_id)->where('status','<>',RepayRecord::STATUS_FOUR)->where('pay_off',RepayRecord::PAY_OFF_ONE)->sum('amount');
        $all_money = bcadd($all_money,$money,2);

        $money_lending_money = RepayRecordMoney::where('summary_id',$summary_id)->where('status','<>',RepayRecordMoney::STATUS_FOUR)->where('period','<>',0)->sum('lending_money');
        $all_money = bcadd($all_money,$money_lending_money,2);

        $common_lending_money = RepayRecordCommon::where('summary_id',$summary_id)->where('status','<>',RepayRecordCommon::STATUS_FOUR)->where('period','<>',0)->sum('lending_money');
        $all_money = bcadd($all_money,$common_lending_money,2);
        $money = bcdiv($money,100,2);
        $all_money = bcdiv($all_money,100,2);
        $returnData['money'] = $money;//还款本金
        $returnData['repay_amount'] = $all_money;//应还金额
        $returnData['lending_money'] = bcsub($all_money,$money,2);//利息费
        return $returnData;
    }
}